//
//  ZZHomeOriginalView.swift
//  web
//
//  Created by 张卓 on 2017/2/4.
//  Copyright © 2017年 ZZ. All rights reserved.
//

// 原创微博

import UIKit

class ZZHomeOriginalView: UIView {

    //记录约束
    var bottomViewConstraint: Constraint?
    
    //提供属性供外界赋值
    var statuViewModel:ZZStatusViewModel? {
        didSet{
            //头像
            headImgView.zz_setImage(urlString: statuViewModel?.homeModel?.user?.profile_image_url)
            //昵称
            nameLabel.text = statuViewModel?.homeModel?.user?.name
            //会员等级
            membershipImageView.image = statuViewModel?.membershipImage
            //时间
//            timeLabel.text = statuViewModel?.homeModel?.created_at
            timeLabel.text = statuViewModel?.sinaTimeStr
            //来自
//            sourceLabel.text = statuViewModel?.homeModel?.source
//            sourceLabel.text = dealSinaSource(source: statuViewModel?.homeModel?.source)
            sourceLabel.text = statuViewModel?.sourceStr
            //用户认证
            avatarImageView.image = statuViewModel?.avatarImage
            //内容
            contentLabel.text = statuViewModel?.homeModel?.text
            //图片
//            print(statuViewModel?.homeModel?.pic_urls)
            // 卸载记录约束
            bottomViewConstraint?.uninstall()
            // 图片不为nil. 且 个数 大于 0
            if let pic = statuViewModel?.homeModel?.pic_urls,pic.count > 0 {
                //有值
                //赋值
                pictureView.pic_urls = pic
                
                //设置约束
                snp_makeConstraints(closure: { (make) in
                    bottomViewConstraint =  make.bottom.equalTo(pictureView).offset(10).constraint
                })
                //显示图片
                pictureView.isHidden = false
            }else{
                //没有值
                //设置约束
                snp_makeConstraints(closure: { (make) in
                   bottomViewConstraint =  make.bottom.equalTo(contentLabel).offset(10).constraint
                })
                //隐藏图片
                pictureView.isHidden = true
            }
            
            
        }
        
    }
    
    
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //MARK: - 设置视图
    private func setupUI(){
        backgroundColor = UIColor.white
        //添加控件
        addSubview(headImgView)
        addSubview(nameLabel)
        addSubview(membershipImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(avatarImageView)
        addSubview(contentLabel)
        //图片
        addSubview(pictureView)
        //设置约束
        headImgView.snp_makeConstraints { (make) in
            make.top.left.equalTo(10)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        nameLabel.snp_makeConstraints { (make) in
            make.top.equalTo(headImgView)
            make.left.equalTo(headImgView.snp_right).offset(10)
        }
        membershipImageView.snp_makeConstraints { (make) in
            make.left.equalTo(nameLabel.snp_right).offset(10)
            make.centerY.equalTo(nameLabel)
        }
        
        timeLabel.snp_makeConstraints { (make) in
            make.left.equalTo(nameLabel)
            make.bottom.equalTo(headImgView)
        }
        
        sourceLabel.snp_makeConstraints { (make) in
            make.left.equalTo(timeLabel.snp_right).offset(10)
            make.top.equalTo(timeLabel)
        }
        
        
        avatarImageView.snp_makeConstraints { (make) in
            make.centerX.equalTo(headImgView.snp_right)
            make.centerY.equalTo(headImgView.snp_bottom)
        }
        
        contentLabel.snp_makeConstraints { (make) in
            make.left.equalTo(10)
            make.width.equalTo(ZZSCREENW - 20)
            make.top.equalTo(headImgView.snp_bottom).offset(10)
            //            make.bottom.equalTo(self).offset(-10)
        }
        
        pictureView.snp_makeConstraints { (make) in
            make.left.equalTo(10)
            make.top.equalTo(contentLabel.snp_bottom).offset(10)
            //由item自动计算size
//            make.size.equalTo(CGSize(width: 100, height: 100))
        }
        
        
        //        // 当前原创微博的底部
        snp_makeConstraints { (make) in
            bottomViewConstraint = make.bottom.equalTo(pictureView).offset(10).constraint
        }
        
    
    }
    //MARK: - 懒加载空间
    ///头像
    private lazy var headImgView : UIImageView = UIImageView(imgName: "avatar_default")
    ///昵称
    private lazy var nameLabel : UILabel = UILabel(fontSize: ZZSMALLFONTSIZE, textColor: UIColor.black)
    //会员等级
    private lazy var membershipImageView : UIImageView = UIImageView(imgName: "common_icon_membership_expired")
    //时间
    private lazy var timeLabel : UILabel = UILabel(fontSize: ZZSMALLFONTSIZE, textColor: ZZThemeColor)
    ///来自
    private lazy var sourceLabel : UILabel = UILabel(fontSize: ZZSMALLFONTSIZE, textColor: ZZThemeColor)
    //用户认证
    private lazy var avatarImageView : UIImageView = UIImageView(imgName: "avatar_vgirl")
    //微博正文
    private lazy var contentLabel : UILabel = {
       let lab = UILabel(fontSize: ZZNORMALFONTSIZE, textColor: UIColor.black)
        //换行
        lab.numberOfLines = 0
        lab.backgroundColor = UIColor.white
        
        return lab
    }()
    
    /// 图片
    private lazy var pictureView :ZZHomePictureView = {
       let view = ZZHomePictureView()
        view.backgroundColor = self.backgroundColor
        return view        
    }()

}
